package com.ice.framework.utils

import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.location.Criteria
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.os.Bundle
import com.blankj.utilcode.util.SPUtils

object LocationUtil {

    @SuppressLint("MissingPermission")
    fun get(activity: Activity, callback: (longitude: Double, latitude: Double) -> Unit) {
        if (SPUtils.getInstance().getString("longitude", "").isNotEmpty()) {
            callback(
                SPUtils.getInstance().getString("longitude", "").toDouble(),
                SPUtils.getInstance().getString("latitude", "").toDouble()
            )
        }
        PermissionUtil.requestPermissions(
            arrayOf(
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION
            ),
            success = {
                if(!LocationUtils.isLocationEnabled()){
                    ToastUtil.show("您的定位服务未开启")
                    return@requestPermissions
                }
                val locationManager =
                    activity.getSystemService(Context.LOCATION_SERVICE) as LocationManager
                val criteria = Criteria()
                criteria.accuracy = Criteria.ACCURACY_COARSE
                criteria.isAltitudeRequired = false
                criteria.isBearingRequired = false
                criteria.isCostAllowed = true
                criteria.powerRequirement = Criteria.POWER_LOW
                val locationProvider = locationManager.getBestProvider(criteria, true)
                val location = locationManager.getLastKnownLocation(locationProvider)
                if (location != null) {
                    if (
                        location.longitude.toString() != SPUtils.getInstance()
                            .getString("longitude", "")
                        && location.latitude.toString() != SPUtils.getInstance()
                            .getString("latitude", "")
                    ) {
                        SPUtils.getInstance().put("longitude", location.longitude.toString())
                        SPUtils.getInstance().put("latitude", location.latitude.toString())
                        callback(location.longitude, location.latitude)
                    }

                }
                locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER,
                    2000,
                    0.toFloat(),
                    object : LocationListener {
                        override fun onLocationChanged(location: Location?) {
                            if (location != null) {
                                if (
                                    location.longitude.toString() != SPUtils.getInstance()
                                        .getString("longitude", "")
                                    && location.latitude.toString() != SPUtils.getInstance()
                                        .getString("latitude", "")
                                ) {
                                    SPUtils.getInstance()
                                        .put("longitude", location.longitude.toString())
                                    SPUtils.getInstance()
                                        .put("latitude", location.latitude.toString())
                                    callback(location.longitude, location.latitude)
                                }
                            }
                        }

                        override fun onStatusChanged(p0: String?, p1: Int, p2: Bundle?) {

                        }

                        override fun onProviderEnabled(p0: String?) {

                        }

                        override fun onProviderDisabled(p0: String?) {

                        }

                    })
            },
            fail = {}
        )
    }
}